<template>
  <div class="article">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="文章详情"
    ></van-nav-bar>

    <!--loading-->
    <div class="loading" v-if="loading">
      <van-loading class="inner" type="spinner" color="#1989fa" />
    </div>

    <!-- 文章详情 -->
    <div class="detail" v-else>
      <h3 class="title">{{ article.title }}</h3>
      <div class="author">
        <van-image
          round
          width="1rem"
          height="1rem"
          fit="fill"
          :src="article.aut_photo"
        />
        <div class="text">
          <p class="name">{{ article.aut_name }}</p>
          <p class="time">{{ article.pubdate | fromNow }}</p>
        </div>
        <van-button round size="small" type="info" @click="follow">
          {{ article.is_followed ? '取消关注' : '+ 关注' }}
        </van-button>
      </div>
      <div class="content">
        <div v-html="article.content"></div>
      </div>

      <!--分割线-->
      <van-divider>END</van-divider>

      <!--按钮-->
      <div class="btns">
        <van-button
          round
          size="small"
          hairline
          type="primary"
          plain
          :icon="article.attitude === 1 ? 'good-job' : 'good-job-o'"
          @click="zanFn"
        >
          {{ article.attitude === 1 ? '取消点赞' : '点赞' }}
        </van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
          round
          size="small"
          hairline
          type="danger"
          plain
          :icon="article.attitude === 0 ? 'delete' : 'delete-o'"
          @click="dislike"
        >
          {{ article.attitude === 0 ? '取消不喜欢' : '不喜欢' }}
        </van-button>
      </div>
    </div>
    <Common></Common>
  </div>
</template>
<script>
import Common from './common.vue'
import {
  addLike,
  deleteLike,
  followUser,
  getArticleDetail,
  unfollowUser,
  deleteDisLike,
  addDisLike
} from '@/api/article'
import { mapState } from 'vuex'
export default {
  name: 'Article',
  data() {
    return {
      loading: true,
      article: {}
    }
  },
  computed: {
    ...mapState('user', ['token']),
    artId() {
      // 路由规则里面定的id
      return this.$route.params.id
    }
  },
  async created() {
    const res = await getArticleDetail(this.artId)
    console.log(res)
    this.article = res.data
    this.loading = false
  },
  methods: {
    async follow() {
      if (!this.token.token) {
        // 未登录
        // this.$router.push('/login')
        // !活的写法带参数
        this.$router.push({
          path: '/login',
          query: {
            flag: true, //! 登录成功的时候，使用back方式回来，不是使用push回来
            // back回来的地址
            back: this.$route.fullPath
          }
        })
        return this.$toast('请先登录')
      }
      // 已登录
      // 判断关注和取关发请求
      if (this.article.is_followed) {
        await unfollowUser(this.article.art_id)
      } else {
        await followUser(this.article.art_id)
      }
      this.article.is_followed = !this.article.is_followed
      this.$toast('操作成功')
    },
    async zanFn() {
      // 判断发请求点赞或取消点赞(先判断登录)
      if (!this.token.token) {
        // 未登录
        // !活的写法带参数
        this.$router.push({
          path: '/login',
          query: {
            flag: true, //! 登录成功的时候，使用back方式回来，不是使用push回来
            // back回来的地址
            back: this.$route.fullPath
          }
        })
        return this.$toast('请先登录')
      }
      if (this.article.attitude === 1) {
        // 取消点赞
        await deleteLike(this.artId)
        this.$toast('取消成功')
        this.article.attitude = -1
      } else {
        // 点赞
        await addLike(this.artId)
        this.$toast('点赞成功')
        this.article.attitude = 1
      }
    },
    async dislike() {
      // 判断发请求点赞或取消点赞(先判断登录)
      if (!this.token.token) {
        // 未登录
        // !活的写法带参数
        this.$router.push({
          path: '/login',
          query: {
            flag: true, //! 登录成功的时候，使用back方式回来，不是使用push回来
            // back回来的地址
            back: this.$route.fullPath
          }
        })
        return this.$toast('请先登录')
      }
      if (this.article.attitude === 0) {
        // 取消
        await deleteDisLike(this.artId)
        this.$toast('取消成功')
        this.article.attitude = 1
      } else {
        // 不喜欢
        await addDisLike(this.artId)
        this.$toast('不喜欢成功')
        this.article.attitude = 0
      }
    }
  },
  components: {
    Common
  }
}
</script>
<style lang="less" scoped>
.loading {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
.article {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.loading {
  padding-top: 100px;
  text-align: center;
}
.error {
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan {
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size: 14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    ::v-deep img {
      max-width: 100%;
      background: #f9f9f9;
    }
  }
  .btns {
    width: 50%;
    margin: 0 auto;
    display: flex;
    justify-content: space-around;
    align-content: center;
  }
}
</style>
